/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.persistence.dao;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.lifebudget.app.client.mvc.model.Category;
import org.lifebudget.app.client.mvc.model.CategoryAmount;
import org.lifebudget.app.client.mvc.model.PaycheckCalculator;
import org.lifebudget.app.client.mvc.model.Transaction;
import org.lifebudget.app.client.mvc.model.Transaction.TransactionType;

import com.google.appengine.api.datastore.Key;

/**
 * The Class TransactionDAO. Data Access Object in for the persistence
 * layer to handle persisting a {@link Transaction}. Annotated for JPA
 * persistence.
 */
@Entity
public class TransactionDAO {
	
	/** The key. */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Key key;
	
	/** The user id. */
	private String userId;
	
	/** The date. */
	private Date date;
	
	/** The description. */
	private String description;
	
	/** The amount. */
	private double amount;
	
	/** The type. */
	private String type;
	
	/** The association name. */
	private String associationName;
	
	/** The category id. */
	private long categoryId;
	
	/** The paycheck id. */
	private long paycheckId;

	/**
	 * Instantiates a new transaction dao.
	 *
	 * @param transaction the transaction
	 */
	public TransactionDAO(Transaction transaction) {
		this.date = transaction.getDate();
		this.description = transaction.getDescription();
		this.amount = transaction.getAmount();
		this.associationName = transaction.getAssociationName();
		this.type = transaction.getType().toString();
		if (transaction.getCategory() != null) {
			this.categoryId = transaction.getCategory().getId();
		}
		if (transaction.getPaycheckCalc() != null) {
			this.paycheckId = transaction.getPaycheckCalc().getId();
		}
	}

	/**
	 * Sets the fields from model object.
	 *
	 * @param transaction the new fields from model object
	 */
	public void setFieldsFromModelObject(Transaction transaction) {
		this.setDate(transaction.getDate());
		this.setDescription(transaction.getDescription());
		this.setAmount(transaction.getAmount());
		this.setType(transaction.getType().toString());
		this.setAssociationName(transaction.getAssociationName());
		if (transaction.getCategory() != null) {
			this.categoryId = transaction.getCategory().getId();
		}
		if (transaction.getPaycheckCalc() != null) {
			this.paycheckId = transaction.getPaycheckCalc().getId();
		}
	}

	/**
	 * To model object.
	 *
	 * @return the transaction
	 */
	public Transaction toModelObject() {
		Transaction t = new Transaction();
		t.setId(this.key.getId());
		t.setDate(this.date);
		t.setDescription(this.description);
		t.setAmount(this.amount);
		t.setType(this.type);
		if (this.paycheckId != 0 && this.type.equals(TransactionType.Paycheck.toString())) {
			PaycheckCalculator pc = new PaycheckCalculator();
			pc.setId(this.paycheckId);
			t.setPaycheckCalc(pc);
		} else if (this.categoryId != 0 && this.type.equals(TransactionType.Categorized.toString())) {
			Category c = new Category();
			c.setId(this.categoryId);
			t.setCategory(c);
		}
		t.setAssociationName(this.associationName);

		return t;
	}

	/**
	 * Sets the key.
	 *
	 * @param key the new key
	 */
	public void setKey(Key key) {
		this.key = key;
	}

	/**
	 * Gets the key.
	 *
	 * @return the key
	 */
	public Key getKey() {
		return key;
	}

	/**
	 * Sets the user id.
	 *
	 * @param userId the new user id
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * Gets the user id.
	 *
	 * @return the user id
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * Sets the date.
	 *
	 * @param date the new date
	 */
	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * Gets the date.
	 *
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * Sets the description.
	 *
	 * @param description the new description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Gets the description.
	 *
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets the amount.
	 *
	 * @param amount the new amount
	 */
	public void setAmount(double amount) {
		this.amount = amount;
	}

	/**
	 * Gets the amount.
	 *
	 * @return the amount
	 */
	public double getAmount() {
		return amount;
	}

	/**
	 * Sets the type.
	 *
	 * @param type the new type
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * Gets the type.
	 *
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * Sets the association name.
	 *
	 * @param associationName the new association name
	 */
	public void setAssociationName(String associationName) {
		this.associationName = associationName;
	}

	/**
	 * Gets the association name.
	 *
	 * @return the association name
	 */
	public String getAssociationName() {
		return associationName;
	}

	/**
	 * Sets the category id.
	 *
	 * @param categoryId the new category id
	 */
	public void setCategoryId(long categoryId) {
		this.categoryId = categoryId;
	}

	/**
	 * Gets the category id.
	 *
	 * @return the category id
	 */
	public long getCategoryId() {
		return categoryId;
	}

	/**
	 * Sets the paycheck id.
	 *
	 * @param paycheckId the new paycheck id
	 */
	public void setPaycheckId(long paycheckId) {
		this.paycheckId = paycheckId;
	}

	/**
	 * Gets the paycheck id.
	 *
	 * @return the paycheck id
	 */
	public long getPaycheckId() {
		return paycheckId;
	}

}
